﻿
@{
    Layout = null;
}

<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>
<div ng-controller="CouponInfoController">
    <div class="container-fluid" ng-show="!selectProduct" id="AgentInfoUpdateMainViewPanel">
        <div class="row">
            <div class="col-md-12">
                <form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
                    <!-- START panel-->
                    <div class="panel panel-default" ng-class="{ 'whirl standard': isLoading }">
                        <div class="panel-body">
                            <fieldset class="b0">
                                <legend>优惠券信息</legend>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">优惠券名称</label>
                                    <div class="col-sm-4">
                                        <input maxlength="50" type="text" name="CouponName" required="" ng-model="model.CouponName" class="form-control" />
                                        <span ng-show="validateInput('CouponName', 'required')" class="text-danger">请输入优惠券名称</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">优惠面额</label>
                                    <div class="col-sm-4">
                                        <input maxlength="10" type="number" name="Amount" required="" ng-model="model.Amount" class="form-control"  />
                                        <span ng-show="validateInput('Amount', 'required')" class="text-danger">请输入优惠面额</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">最低消费金额</label>
                                    <div class="col-sm-4">
                                        <input maxlength="10" type="number" name="Consumption" required="" ng-model="model.Consumption" class="form-control"  />
                                        <span ng-show="validateInput('Consumption', 'required')" class="text-danger">请输入最低消费金额</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">发放数量</label>
                                    <div class="col-sm-4">
                                        <p class="input-group">
                                            <input maxlength="10" type="number" name="TotalNum" required="" ng-model="model.TotalNum" class="form-control" />发放数量限制(默认为0则无限制)
                                        </p>
                                            <span ng-show="validateInput('TotalNum', 'required')" class="text-danger">请输入发放数量</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">使用开始日期</label>
                                    <div class="col-sm-4">
                                        <p class="input-group">
                                            <input maxlength="128" type="text" name="StartTime" required="" ng-model="model.StartTime" class="form-control" datetime-picker="yyyy-MM-dd" is-open="picker1.open" datepicker-options="datepickerOptions" />
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker1')"><i class="fa fa-calendar"></i></button>
                                            </span>
                                        </p>
                                        <span ng-show="validateInput('StartTime', 'required')" class="text-danger">请输入使用开始日期</span>
                                    </div>
                                    
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">使用结束日期</label>
                                    <div class="col-sm-4">
                                        <p class="input-group">
                                            <input style="z-index: 0;" name="EndTime" required="" type="text" class="form-control" datetime-picker="yyyy-MM-dd" ng-model="model.EndTime"
                                                   is-open="picker2.open" datepicker-options="datepickerOptions" />
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker2')"><i class="fa fa-calendar"></i></button>
                                            </span>
                                        </p>
                                        <span ng-show="validateInput('EndTime', 'required')" class="text-danger">请输入使用结束日期</span>
                                    </div>
                                    <div class="col-sm-4">
                                        <h4 class="text-danger">*</h4>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">可用分类</label>
                                    <div class="col-sm-4">
                                        <select class="form-control" ng-model="model.Category" ng-options="c.Id as c.CategoryName for c in CategoryList"></select>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">可以与积分同时使用</label>
                                    <div class="col-sm-4">
                                        <label class="switch switch-lg">
                                            <input type="checkbox"  ng-model="model.IsUsePoint">
                                            <span></span>
                                        </label>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">可以用于营销活动</label>
                                    <div class="col-sm-4">
                                        <label class="switch switch-lg">
                                            <input type="checkbox"  ng-model="model.IsUseActivity">
                                            <span></span>
                                        </label>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">可领取</label>
                                    <div class="col-sm-4">
                                        <label class="switch switch-lg">
                                            <input type="checkbox"  ng-model="model.IsOpenReceive">
                                            <span></span>
                                        </label>（是否可在微信端优惠券列表领取）
                                    </div>
                                </div>
                            </fieldset>

                        </div>
                        <div class="panel-footer text-right">
                            <button type="button" class="btn btn-warning" ng-click="cancel()">返回列表</button>
                            <button type="button" class="btn btn-primary" ng-click="save()">保存</button>
                        </div>
                    </div>
                    <!-- END panel-->
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    App.controller('CouponInfoController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter",
        function ($scope, $state, $http, $stateParams, Notify, $filter) {
            'use strict';
            $scope.submitted = false;
            $scope.isLoading = false;
            $scope.validateInput = function (name, type) {
                var input = $scope.formValidate[name];
                return (input.$dirty || $scope.submitted) && input.$error[type];
            };
            $scope.model = {};
            $scope.model.IsOpenReceive = false;
            $scope.model.IsUsePoint = false;
            $scope.model.IsUseActivity = false;
            $scope.model.Category = 0;
            $scope.model.TotalNum = 0;
            $scope.CategoryList = [];

            // 日期插件
            $scope.picker1 = {
                date: new Date()
            };

            $scope.picker2 = {
                date: new Date()
            };

            $scope.datepickerOptions = {
                maxDate: null
            }

            $scope.openCalendar = function (e, picker) {
                $scope[picker].open = true;
            };

            //数据初始化
            if ($stateParams.id && $stateParams.id > 0) {
                $scope.isLoading = true;
                $http.post("/Coupon/GetCouponInfo", { id: $stateParams.id }).then(function (response) {
                    $scope.model = response.data.data;
                    if (!$scope.model) {
                        Notify.alert("优惠券不存在", { status: 'warning' });
                        $state.go('app.couponlist');
                        $scope.model = {};
                    }
                    //$rootScope.initArea($scope.AgentInfo.HomeProvince, $scope.AgentInfo.HomeCity);
                    $scope.isLoading = false;
                });
            }


            
            $http.post("/Coupon/GetCategoryList").then(function (response) {
                console.log(response)
                if (response.data.isSuccess) {
                    $scope.CategoryList = response.data.data;

                }
                
            });
            // 提交
            $scope.save = function () {
                $scope.submitted = true;
                $scope.isLoading = true;
                if ($scope.formValidate.$valid) {
                    $http.post("/Coupon/SaveCoupon", { model: $scope.model }).then(function (returnData) {
                        if (returnData.data.isSuccess) {
                            Notify.alert('保存成功!', { status: 'success' });
                            $state.go("app.couponlist");
                        } else {
                            Notify.alert(returnData.data.msg, { status: 'warning' });
                            $scope.isLoading = false;
                        }
                    });
                } else {
                    $scope.isLoading = false;
                }
            };

            //返回列表
            $scope.cancel = function () {
                $state.go('app.couponlist');
            }
        }]);
</script>
